<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>CMakeAddFortranSubdirectory &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="CMakeBackwardCompatibilityCXX" href="CMakeBackwardCompatibilityCXX.html" />
    <link rel="prev" title="CheckVariableExists" href="CheckVariableExists.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="CMakeBackwardCompatibilityCXX.html" title="CMakeBackwardCompatibilityCXX"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="CheckVariableExists.html" title="CheckVariableExists"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CMakeAddFortranSubdirectory</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="cmakeaddfortransubdirectory">
<span id="module:CMakeAddFortranSubdirectory"></span><h1>CMakeAddFortranSubdirectory<a class="headerlink" href="#cmakeaddfortransubdirectory" title="Link to this heading">¶</a></h1>
<p>This module provides a command to add a Fortran project located in a
subdirectory.</p>
<p>Load it in a CMake project with:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CMakeAddFortranSubdirectory</span><span class="nf">)</span>
</pre></div>
</div>
<section id="commands">
<h2>Commands<a class="headerlink" href="#commands" title="Link to this heading">¶</a></h2>
<p>This module provides the following command:</p>
<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:cmake_add_fortran_subdirectory">
<span class="sig-name descname"><span class="pre">cmake_add_fortran_subdirectory</span></span><a class="headerlink" href="#command:cmake_add_fortran_subdirectory" title="Link to this definition">¶</a></dt>
<dd><p>Adds a Fortran-only subproject from subdirectory to the current project:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">cmake_add_fortran_subdirectory(</span>
<span class="w">  </span><span class="nv">&lt;subdir&gt;</span>
<span class="w">  </span><span class="no">PROJECT</span><span class="w"> </span><span class="nv">&lt;project-name&gt;</span>
<span class="w">  </span><span class="no">ARCHIVE_DIR</span><span class="w"> </span><span class="nv">&lt;dir&gt;</span>
<span class="w">  </span><span class="no">RUNTIME_DIR</span><span class="w"> </span><span class="nv">&lt;dir&gt;</span>
<span class="w">  </span><span class="no">LIBRARIES</span><span class="w"> </span><span class="nv">&lt;libs&gt;...</span>
<span class="w">  </span><span class="no">LINK_LIBRARIES</span>
<span class="w">    </span><span class="p">[</span><span class="no">LINK_LIBS</span><span class="w"> </span><span class="nv">&lt;lib&gt;</span><span class="w"> </span><span class="nv">&lt;deps&gt;...</span><span class="p">]...</span>
<span class="w">  </span><span class="p">[</span><span class="no">CMAKE_COMMAND_LINE</span><span class="w"> </span><span class="nv">&lt;flags&gt;...</span><span class="p">]</span>
<span class="w">  </span><span class="no">NO_EXTERNAL_INSTALL</span>
<span class="nf">)</span>
</pre></div>
</div>
<p>This command checks whether the current compiler supports Fortran or attempts
to locate a Fortran compiler.  If a compatible Fortran compiler is found, the
Fortran project located in <code class="docutils literal notranslate"><span class="pre">&lt;subdir&gt;</span></code> is added as a subdirectory to the
current project.</p>
<p>If no Fortran compiler is found and the compiler is <code class="docutils literal notranslate"><span class="pre">MSVC</span></code>, it searches for
the MinGW <code class="docutils literal notranslate"><span class="pre">gfortran</span></code> compiler.  In this case, the Fortran project is built
as an external project using MinGW tools, and Fortran-related imported targets
are created.  This setup works only if the Fortran code is built as a shared
DLL library, so the <span class="target" id="index-0-variable:BUILD_SHARED_LIBS"></span><a class="reference internal" href="../variable/BUILD_SHARED_LIBS.html#variable:BUILD_SHARED_LIBS" title="BUILD_SHARED_LIBS"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">BUILD_SHARED_LIBS</span></code></a> variable is enabled in the
external project.  Additionally, the <span class="target" id="index-0-variable:CMAKE_GNUtoMS"></span><a class="reference internal" href="../variable/CMAKE_GNUtoMS.html#variable:CMAKE_GNUtoMS" title="CMAKE_GNUtoMS"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_GNUtoMS</span></code></a> variable is set
to <code class="docutils literal notranslate"><span class="pre">ON</span></code> to ensure that Microsoft-compatible <code class="docutils literal notranslate"><span class="pre">.lib</span></code> files are created.</p>
<p>The options are:</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">PROJECT</span></code></dt><dd><p>The name of the Fortran project as defined in the top-level
<code class="docutils literal notranslate"><span class="pre">CMakeLists.txt</span></code> located in <code class="docutils literal notranslate"><span class="pre">&lt;subdir&gt;</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">ARCHIVE_DIR</span></code></dt><dd><p>Directory where the project places <code class="docutils literal notranslate"><span class="pre">.lib</span></code> archive files.  A relative path
is interpreted as relative to <span class="target" id="index-0-variable:CMAKE_CURRENT_BINARY_DIR"></span><a class="reference internal" href="../variable/CMAKE_CURRENT_BINARY_DIR.html#variable:CMAKE_CURRENT_BINARY_DIR" title="CMAKE_CURRENT_BINARY_DIR"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_CURRENT_BINARY_DIR</span></code></a>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">RUNTIME_DIR</span></code></dt><dd><p>Directory where the project places <code class="docutils literal notranslate"><span class="pre">.dll</span></code> runtime files.  A relative path
is interpreted as relative to <span class="target" id="index-1-variable:CMAKE_CURRENT_BINARY_DIR"></span><a class="reference internal" href="../variable/CMAKE_CURRENT_BINARY_DIR.html#variable:CMAKE_CURRENT_BINARY_DIR" title="CMAKE_CURRENT_BINARY_DIR"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_CURRENT_BINARY_DIR</span></code></a>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">LIBRARIES</span></code></dt><dd><p>Names of library targets to create or import into the current project.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">LINK_LIBRARIES</span></code></dt><dd><p>Specifies link interface libraries for <code class="docutils literal notranslate"><span class="pre">LIBRARIES</span></code>.  This option expects a
list of <code class="docutils literal notranslate"><span class="pre">LINK_LIBS</span> <span class="pre">&lt;lib&gt;</span> <span class="pre">&lt;deps&gt;...</span></code> items, where:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">LINK_LIBS</span></code> marks the start of a new pair</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;lib&gt;</span></code> is a library target.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;deps&gt;...</span></code> represents one or more dependencies required by <code class="docutils literal notranslate"><span class="pre">&lt;lib&gt;</span></code>.</p></li>
</ul>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_COMMAND_LINE</span></code></dt><dd><p>Additional command-line flags passed to <span class="target" id="index-0-manual:cmake(1)"></span><a class="reference internal" href="../manual/cmake.1.html#manual:cmake(1)" title="cmake(1)"><code class="xref cmake cmake-manual docutils literal notranslate"><span class="pre">cmake(1)</span></code></a> command when
configuring the Fortran subproject.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">NO_EXTERNAL_INSTALL</span></code></dt><dd><p>Prevents installation of the external project.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">NO_EXTERNAL_INSTALL</span></code> option is required for forward compatibility
with a future version that supports installation of the external project
binaries during <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">install</span></code>.</p>
</div>
</dd>
</dl>
</dd></dl>

</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>Adding a Fortran subdirectory to a project can be done by including this module
and calling the <code class="docutils literal notranslate"><span class="pre">cmake_add_fortran_subdirectory()</span></code> command.  In the following
example, a Fortran project provides the <code class="docutils literal notranslate"><span class="pre">hello</span></code> library and its dependent
<code class="docutils literal notranslate"><span class="pre">world</span></code> library:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CMakeAddFortranSubdirectory</span><span class="nf">)</span>

<span class="nf">cmake_add_fortran_subdirectory(</span>
<span class="w">  </span><span class="nb">fortran-subdir</span>
<span class="w">  </span><span class="no">PROJECT</span><span class="w"> </span><span class="nb">FortranHelloWorld</span>
<span class="w">  </span><span class="no">ARCHIVE_DIR</span><span class="w"> </span><span class="nb">lib</span>
<span class="w">  </span><span class="no">RUNTIME_DIR</span><span class="w"> </span><span class="nb">bin</span>
<span class="w">  </span><span class="no">LIBRARIES</span><span class="w"> </span><span class="nb">hello</span><span class="w"> </span><span class="nb">world</span>
<span class="w">  </span><span class="no">LINK_LIBRARIES</span>
<span class="w">    </span><span class="no">LINK_LIBS</span><span class="w"> </span><span class="nb">hello</span><span class="w"> </span><span class="nb">world</span><span class="w"> </span><span class="c"># hello library depends on the world library</span>
<span class="w">  </span><span class="no">NO_EXTERNAL_INSTALL</span>
<span class="nf">)</span>

<span class="c"># The Fortran target can be then linked to the main project target.</span>
<span class="nf">add_executable(</span><span class="nb">main</span><span class="w"> </span><span class="nb">main.c</span><span class="nf">)</span>
<span class="nf">target_link_libraries(</span><span class="nb">main</span><span class="w"> </span><span class="no">PRIVATE</span><span class="w"> </span><span class="nb">hello</span><span class="nf">)</span>
</pre></div>
</div>
</section>
<section id="see-also">
<h2>See Also<a class="headerlink" href="#see-also" title="Link to this heading">¶</a></h2>
<p>There are multiple ways to integrate Fortran libraries.  Alternative approaches
include:</p>
<ul class="simple">
<li><p>The <span class="target" id="index-0-command:add_subdirectory"></span><a class="reference internal" href="../command/add_subdirectory.html#command:add_subdirectory" title="add_subdirectory"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">add_subdirectory()</span></code></a> command to add the subdirectory directly to
the build.</p></li>
<li><p>The <span class="target" id="index-0-command:export"></span><a class="reference internal" href="../command/export.html#command:export" title="export"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">export()</span></code></a> command can be used in the subproject to provide
<a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">Imported Targets</span></a> or similar for integration with other projects.</p></li>
<li><p>The <span class="target" id="index-0-module:FetchContent"></span><a class="reference internal" href="FetchContent.html#module:FetchContent" title="FetchContent"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">FetchContent</span></code></a> or <span class="target" id="index-0-module:ExternalProject"></span><a class="reference internal" href="ExternalProject.html#module:ExternalProject" title="ExternalProject"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">ExternalProject</span></code></a> modules when working
with external dependencies.</p></li>
</ul>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">CMakeAddFortranSubdirectory</a><ul>
<li><a class="reference internal" href="#commands">Commands</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#see-also">See Also</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="CheckVariableExists.html"
                          title="previous chapter">CheckVariableExists</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="CMakeBackwardCompatibilityCXX.html"
                          title="next chapter">CMakeBackwardCompatibilityCXX</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/CMakeAddFortranSubdirectory.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="CMakeBackwardCompatibilityCXX.html" title="CMakeBackwardCompatibilityCXX"
             >next</a> |</li>
        <li class="right" >
          <a href="CheckVariableExists.html" title="CheckVariableExists"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CMakeAddFortranSubdirectory</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>